//
//  RadioCollectionViewController.m
//  NEWS_W_Y_S
//
//  Created by lanou3g on 15/10/15.
//  Copyright © 2015年 lanou3g.jake. All rights reserved.
//

#import "RadioCollectionViewController.h"

#import "RadioCollectionViewCell.h"
#import "RadioModel.h"
#import "HeaderCollectionReusableView.h"
#import "MJRefresh.h"
#import "RadioHelp.h"
#import "UIImageView+WebCache.h"
#import "RadioSortTableViewController.h"

#define RaioURL @"http://c.3g.163.com/nc/topicset/ios/radio/index.html"

@interface RadioCollectionViewController ()<UICollectionViewDelegateFlowLayout>

@property(nonatomic,strong)NSMutableArray *allRadioArray;

@end

@implementation RadioCollectionViewController

static NSString * const reuseIdentifier = @"Cell";

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.laout.scrollDirection = UICollectionViewScrollDirectionVertical;//设置竖直滚动方向
    self.laout.minimumInteritemSpacing = 10;// 设置全局的cell的间距
    self.laout.minimumLineSpacing = 50; //设置全局的行间距
    
     //设定页眉和页脚的全局尺寸，需要注意的是，根据滚动方向不同，header和footer的width和height中只有一个会起作用
    self.laout.headerReferenceSize = CGSizeMake(25, 40);

    [self.collectionView registerNib:[UINib nibWithNibName:@"RadioCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:reuseIdentifier];
    
    [self.collectionView registerClass:[HeaderCollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header"];
    
    self.collectionView.backgroundColor = [UIColor whiteColor];
    
    //请求数据
    [self requestData];
    //加载刷新
    [self refrshHearAndFoot];
    
    
}
//数据
-(void)requestData{
    
    [[RadioHelp shareRadioData] requestWithUrl:RaioURL block:^(NSMutableArray *array) {
        self.allRadioArray = array;
        [self.collectionView reloadData];
    }];
}


//刷新
-(void)refrshHearAndFoot{

    //下拉
    self.collectionView.header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(headerRefreshing)];
    
    
    [self.collectionView.header beginRefreshing];
    //上拉
    self.collectionView.footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(footerRefreshing)];
    
}

-(void)headerRefreshing{
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [self requestData];
       [self.collectionView.header endRefreshing];
    });
    
}

-(void)footerRefreshing{
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [self requestData];
      [self.collectionView.footer endRefreshing];
    });
   
}


-(UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    HeaderCollectionReusableView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header" forIndexPath:indexPath];
    headerView.button.tag = 3000 + indexPath.section;
    RadioModel *radioModel = self.allRadioArray[indexPath.section];
    [headerView headerData:radioModel];

    [headerView.button addTarget:self action:@selector(nextVC:) forControlEvents:UIControlEventTouchUpInside];
    
    
    return headerView;
}

-(void)nextVC:(UIButton *)button{
    
   // NSLog(@"交定金");

    RadioSortTableViewController *sortVC = [[RadioSortTableViewController alloc] init];
    sortVC.hidesBottomBarWhenPushed = YES;
    
    sortVC.cid = ((RadioModel *)self.allRadioArray[button.tag - 3000]).cid;
    sortVC.cname = ((RadioModel *)self.allRadioArray[button.tag - 3000]).cname;
    
    [self.navigationController pushViewController:sortVC animated:YES];
    
}
//分组cell的高度

-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return CGSizeMake(110, 230);
}
//分类的详情位置大小
-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
    return UIEdgeInsetsMake(0, 10, 0, 10);
}

#pragma mark <UICollectionViewDataSource>

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {

 // NSLog(@"00000%ld",self.allRadioArray.count);
    return self.allRadioArray.count;  //分类的组数
}


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {

    return 3;  //分组展示的个数
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    RadioCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath];
    
    RadioModel *radioModel = self.allRadioArray[indexPath.section];
   // NSLog(@"+++%@",radioModel);
    [cell.radioImageView sd_setImageWithURL:radioModel.tList[indexPath.row][@"radio"][@"imgsrc"] placeholderImage:[UIImage imageNamed:@"picholder"]];
    
    cell.radioImageView.layer.masksToBounds = YES;
    
    NSDictionary *dic = radioModel.tList[indexPath.row][@"radio"];
   // NSLog(@"----%@",dic);
    cell.radioNameLabel.text = dic[@"tname"];
  //  NSLog(@"%@",dic[@"tname"]);
  //  NSLog(@"%@",cell.radioNameLabel.text);
    cell.radioIntroduce.text = dic[@"title"];
    
    cell.playCountLabel.text = [NSString stringWithFormat:@"%@", radioModel.tList[indexPath.row][@"playCount"]];

    return cell;
}

#pragma mark <UICollectionViewDelegate>

/*
// Uncomment this method to specify if the specified item should be highlighted during tracking
- (BOOL)collectionView:(UICollectionView *)collectionView shouldHighlightItemAtIndexPath:(NSIndexPath *)indexPath {
	return YES;
}
*/

/*
// Uncomment this method to specify if the specified item should be selected
- (BOOL)collectionView:(UICollectionView *)collectionView shouldSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    return YES;
}
*/

/*
// Uncomment these methods to specify if an action menu should be displayed for the specified item, and react to actions performed on the item
- (BOOL)collectionView:(UICollectionView *)collectionView shouldShowMenuForItemAtIndexPath:(NSIndexPath *)indexPath {
	return NO;
}

- (BOOL)collectionView:(UICollectionView *)collectionView canPerformAction:(SEL)action forItemAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender {
	return NO;
}

- (void)collectionView:(UICollectionView *)collectionView performAction:(SEL)action forItemAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender {
	
}
*/

@end
